<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加用户</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="userProfileStore.loading.list || userProfileStore.loading.update"
        :data="userProfileStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="昵称" prop="nickname" />
        <el-table-column label="头像">
          <template #default="{ row }">
            <el-avatar :src="row.avatar_url" />
          </template>
        </el-table-column>
        <el-table-column label="是否管理员" prop="is_active">
          <template #default="{ row }">
            <el-tag :type="row.is_staff ? 'success' : 'danger'">
              {{ row.is_staff ? "是" : "否" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否超级管理员" prop="is_active">
          <template #default="{ row }">
            <el-tag :type="row.is_superuser ? 'success' : 'danger'">
              {{ row.is_superuser ? "是" : "否" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :wdith="500" label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-button type="warning" link @click="handleToEditPassword(row)">修改密码</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="userProfileStore.totalCount"
        @change="userProfileStore.fetchUserProfileList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="userProfileStore.fetchUserProfileList(params)"
    />

    <el-dialog v-model="editPasswordVisible" title="修改密码">
      <el-form ref="editPasswordFormRef" :model="editPasswordForm" :rules="rules">
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="editPasswordForm.new_pwd" type="password" />
        </el-form-item>
        <div class="flex justify-end">
          <el-button @click="editPasswordVisible = false">取消</el-button>
          <el-button type="primary" @click="handleEditPasswordConfirm">确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  EditUserPassowrdForm,
  type UserProfileListParams,
  type UserProfileType,
} from "@/api/user/profile";
import { useUserProfileStoreHook } from "@/store/modules/user/profile";
import Form, { type FormType } from "./components/form.vue";

const userProfileStore = useUserProfileStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<UserProfileType>({
  id: 0,
  username: "",
  nickname: "",
  avatar_url: "",
  balance: "",
  is_staff: false,
  is_superuser: false,
});

const editPasswordFormRef = ref();

const editPasswordForm = reactive<EditUserPassowrdForm>({
  user_id: 0,
  new_pwd: "",
});

const editPasswordVisible = ref(false);

const params = reactive<UserProfileListParams>({
  page: 1,
  size: 10,
});

const rules = {
  new_pwd: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    {
      pattern: /^[a-zA-Z].{7,}$/,
      message: "密码必须至少8位且以字母开头",
      trigger: "blur",
    },
  ],
};

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: UserProfileType) {
  await userProfileStore.fetchDeleteUserProfile(row.id as number);
  await userProfileStore.fetchUserProfileList(params);
}

function handleToEdit(row: UserProfileType) {
  formType.value = "edit";
  currentForm.value = row;
  visible.value = true;
}

function handleToEditPassword(row: UserProfileType) {
  editPasswordForm.user_id = row.id as number;
  editPasswordVisible.value = true;
}

function handleEditPasswordConfirm() {
  editPasswordFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      await userProfileStore.fetchEditUserPassowrd(editPasswordForm);
      editPasswordVisible.value = false;
      ElMessage.success("修改成功");
    }
  });
}

onMounted(() => {
  userProfileStore.fetchUserProfileList(params);
});
</script>

<style scoped lang="scss"></style>
